<?php
/**
 * 后台用户管理控制器
 *
 * @author demon<zhangwei@vip.deyi.com>
 * @since   2018/03/15 09:38
 * @copyright  deyi.com
 */

namespace Apps\Acp\Controllers;

use Apps\Acp\Models\Hexiao;
use Apps\Acp\Models\Order;
use Apps\Acp\Models\User;
use Apps\Common\Constant\BusinessConstant;
use Apps\Common\Controller\AcpBaseController;
use Deyi\Functions\PageFunction;
use Deyi\Functions\UtilFunction;

/**
 * Class ManagerController
 * @package Apps\Acp\Controllers
 */
class UserController extends AcpBaseController
{
    public function indexAction()
    {
        $page = PageFunction::getPageAndOffset($this->getParamValue('page', 1, 'int'), BusinessConstant::PAGE_SIZE);
        $keyword = $this->getParamValue('keyword', '');
        $buy = $this->getParamValue('buy', 0);
        $trade = $this->getParamValue('trade', 0);
        $views = $this->getParamValue('views', 0);
        $conditions = [];

        if ($keyword) {
            $conditions['nickname%'] = $keyword;
        }

        if($buy){
            $conditions['buy_num>'] = 0;
        }

        if($trade){
            $conditions['trade_num>'] = 0;
        }

        if($views){
            $conditions['views>'] = 0;
        }

        $total = User::getInstance()->countListByConds($conditions);
        $list = [];
        if ($total > 0) {
            $list = User::getInstance()->getListByConds($conditions, $page['offset'], $page['size']);
        }

        $this->view->setVars([
            'list' => $list,
            'keyword' => $keyword,
            'buy' => $buy,
            'trade' => $trade,
            'views' => $views,
            'pageHtml' => PageFunction::getPageHtml(
                $page['page'],
                $total,
                $page['size'],
                parse_url($this->request->getURI())['path'],
                [
                    'keyword' => $keyword,
                    'buy' => $buy,
                    'trade' => $trade,
                    'views' => $views
                ],
                4
            )
        ]);
    }

    public function viewsAction()
    {
        $id = $this->getParamValue('id', 0, 'int');

        $data = null;
        $trade_list = null;
        $hx_list = null;
        if ($id) {
            $data = User::getInstance()->getOneByFields(['id' => $id]);

            //成交记录
            $trade_list = Order::getInstance()->getListByConds(['from_uid' => $id, 'uid!=' => $id], 0, BusinessConstant::PAGE_SIZE);

            //核销记录
            $hx_list = Hexiao::getInstance()->getListByConds(['uid' => $id], 0, BusinessConstant::PAGE_SIZE);
        }

        $this->view->setVars([
            'data' => $data,
            'trade_list' => $trade_list,
            'hx_list' => $hx_list
        ]);
    }

    public function hexiaoAction()
    {
        $uid = $this->getParamValue('uid');
        $card_num = $this->getParamValue('card_num', 0);
        $day_num = $this->getParamValue('day_num', 0);
        $cn = $this->getParamValue('cn');

        $user = User::getInstance()->getOneByField('id', $uid);

        if (($user->buy_num - $user->card_num) < $card_num || ($user->award - $user->plus_num) < $day_num) {
            static::errorThrow('超过核销范围');
        }

        if (User::getInstance()->hexiao($uid, $card_num, $day_num)) {
            Hexiao::getInstance()->insertByFields(['uid' => $uid, 'card_num' => $card_num, 'day_num' => $day_num, 'cn' => $cn, 'dateline' => time()]);
        }

        parent::renderSuccessJson();
    }

    public function exportAction()
    {
        $oid = $this->getParamValue('oid', '');
        $cid = $this->getParamValue('cid', '');
        $status = $this->getParamValue('status', '');
        $conditions = [];

        if ($oid) {
            $conditions['oid%'] = $oid;
        }
        if ($cid) {
            $conditions['cid'] = $cid;
        }
        if ($status >= -1 && $status <= 3 && $status != '') {
            $conditions['status'] = $status;
        }

        $total = RebateOrderList::getInstance()->countListByConds($conditions);
        $data = RebateOrderList::getInstance()->getListByConds($conditions, 0, $total)->toArray();
        $orderstatus = BusinessConstant::ORDER_STATUS;
        $content = "活动名,订单号,商家名,商品名,用户名,用户手机,用户地址,购买金额,购买时间,支付时间,OPENID,UNIONID,状态\n";
        for ($i = 0; $i < count($data); $i++) {
            $content .= $data[$i]['campaign_name'] . ",";
            $content .= $data[$i]['oid'] . ",";
            $content .= $data[$i]['business_name'] . ",";
            $content .= $data[$i]['goodsname'] . ",";
            $content .= $data[$i]['username'] . ",";
            $content .= $data[$i]['mobile'] . ",";
            $content .= $data[$i]['address'] . ",";
            $content .= $data[$i]['buy_rmb'] . ",";
            $content .= date("Y-m-d H:i:s", $data[$i]['buy_time']) . ",";
            $content .= date("Y-m-d H:i:s", $data[$i]['pay_time']) . ",";
            $content .= $data[$i]['openid'] . ",";
            $content .= $data[$i]['unionid'] . ",";
            $content .= $orderstatus[$data[$i]['status']] . "\n";
        }
        UtilFunction::exportCsv('order_' . date("YmdHis") . '.csv', $content);

        exit;
    }

}